﻿using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using Resn.WhuPub.BLL;
using System.Collections.ObjectModel;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// SystemPage.xaml 的交互逻辑
        /// </summary>
        public partial class AdminBusinessPage : Page
        {
                private readonly GlobalManager _gm = GlobalManager.Instance;

                public AdminBusinessPage()
                {
                        InitializeComponent();
                }

                private void Page_Loaded(object sender, RoutedEventArgs e)
                {
                        LayoutRoot.DataContext = _gm;
                }

                private void ManageEmployeesButton_Click(object sender, RoutedEventArgs e)
                {
                        _gm.Employees = new ObservableCollection<Employee>(_gm.EmployeeEntityManager.Entitys);
                        NavigationService.Navigate(new Uri("ManageEmployeesPage.xaml", UriKind.Relative));
                }

                private void ImportCorporatesButton_Click(object sender, RoutedEventArgs e)
                {
                        NavigationService.Navigate(new Uri("ImportCorporatesPage.xaml", UriKind.Relative));
                }

                private void ManageCorporatesButton_OnClick(object sender, RoutedEventArgs e)
                {
                        _gm.Corporates = new ObservableCollection<Corporate>(_gm.CorporateEntityManager.Entitys);
                        NavigationService.Navigate(new Uri("ManageCorporatesPage.xaml", UriKind.Relative));
                }

                private void ManageBranchsButton_OnClick(object sender, RoutedEventArgs e)
                {
                        _gm.Branchs = new ObservableCollection<Branch>(_gm.BranchEntityManager.Entitys);
                        NavigationService.Navigate(new Uri("ManageBranchsPage.xaml", UriKind.Relative));
                }

                

                

                private void ImportContactsButton_Click(object sender, RoutedEventArgs e)
                {
                        NavigationService.Navigate(new Uri("ImportContactsPage.xaml", UriKind.Relative));
                }

                private void ManageContactsButton_Click(object sender, RoutedEventArgs e)
                {
                        _gm.Contacts = new ObservableCollection<Contact>(_gm.ContactEntityManager.Entitys);
                        NavigationService.Navigate(new Uri("ManageContactsPage.xaml", UriKind.Relative));
                }

                private void ImportCategorysButton_Click(object sender, RoutedEventArgs e)
                {
                        bool isSuccess = true;
                        try
                        {
                                var openFileDialog = new OpenFileDialog
                                {
                                        Filter = "Excel 文件|*.xls",
                                        Multiselect = false
                                };

                                if (!openFileDialog.ShowDialog().GetValueOrDefault())
                                {
                                        return;
                                }

                                var categorys = Category.LoadCategorysFromExcel(openFileDialog.FileName);

                                foreach (var category in categorys.Keys)
                                {
                                        category.Insert();

                                        foreach (var subject in categorys[category].Keys)
                                        {
                                                subject.Insert();

                                                foreach (var major in categorys[category][subject])
                                                {
                                                        major.Insert();
                                                }
                                        }
                                }
                        }
                        catch (Exception ex)
                        {
                                isSuccess = false;
                                MessageBox.Show(ex.Message);
                        }

                        if (isSuccess)
                        {
                                _gm.NotifyPropertyChanged(i=>i.CategoryEntityManager.Entitys.Count);
                                _gm.NotifyPropertyChanged(i => i.SubjectEntityManager.Entitys.Count);
                                _gm.NotifyPropertyChanged(i => i.MajorEntityManager.Entitys.Count);

                                MessageBox.Show("导入成功");


                        }
                }

                private void ManageCategorysButton_OnClick(object sender, RoutedEventArgs e)
                {
                        _gm.Categorys = new ObservableCollection<Category>(_gm.CategoryEntityManager.Entitys);
                        NavigationService.Navigate(new Uri("ManageCategorysPage.xaml", UriKind.Relative));
                }

                private void ManageSubjectsButton_Click(object sender, RoutedEventArgs e)
                {
                        _gm.Subjects = new ObservableCollection<Subject>(_gm.SubjectEntityManager.Entitys);
                        NavigationService.Navigate(new Uri("ManageSubjectsPage.xaml", UriKind.Relative));
                }

                private void ManageMajorsButton_Click(object sender, RoutedEventArgs e)
                {
                        _gm.Majors = new ObservableCollection<Major>(_gm.MajorEntityManager.Entitys);
                        NavigationService.Navigate(new Uri("ManageMajorsPage.xaml", UriKind.Relative));
                }

                private void ImportBooksButton_Click(object sender, RoutedEventArgs e)
                {
                }

                private void ManageBooksButton_Click(object sender, RoutedEventArgs e)
                {
                        _gm.Books = new ObservableCollection<Book>(_gm.BookEntityManager.Entitys);
                        NavigationService.Navigate(new Uri("ManageBooksPage.xaml", UriKind.Relative));
                }

                
        }
}